﻿using System;
using System.Text.RegularExpressions;
using com.fukefu.source;
using com.fukefu.tools;

namespace com.fukefu.info.b2b.destoon
{
    public class destoon
    {
        Source source = new Source();
        Site site = null;
        ProductInformation pif = null;
        string url = "", login_url = "", post_url = "";

        public bool Login(string name, string pass, string vcode)
        {
            ParametersData pd = new ParametersData();
            pd.Add("forward", url);
            pd.Add("option", "username");
            pd.Add("username", name);
            pd.Add("password", pass);
            pd.Add("cookietime", "2592000");
            pd.Add("goto", "1");
            pd.Add("submit", " 登 录 ");

            string html = source.Post(login_url, pd.Read(), login_url);
            if (html.IndexOf("class=\"n\">退出登录</a></li></ul>") != -1)
            {
                return true;
            }
            return false;
        }

        public InformationResult PostArticle(string title, string content, string vcode)
        {
            InformationResult result = new InformationResult();
            ParametersData pd = new ParametersData();
            pd.Add("action", "add");
            pd.Add("mid", "5");
            pd.Add("itemid", "0");
            pd.Add("forward", url + "/member/");
            pd.Add("post[typeid]", "0");
            pd.Add("post[title]", title);
            pd.Add("style", "");
            pd.Add("post[catid]", site.TypeId[0]);
            pd.Add("post[model]", pif.Get("产品名称"));
            pd.Add("post[standard]", pif.Get("产品规格"));
            pd.Add("post[brand]", pif.Get("产品名称"));
            pd.Add("post[content]", content);
            pd.Add("post[thumb]", UpImage());
            pd.Add("post[totime]", "");
            pd.Add("post[unit]", pif.Get("单位"));
            pd.Add("post[price]", pif.Get("价格"));
            pd.Add("post[minamount]", pif.Get("库存数量"));
            pd.Add("post[amount]", "");
            pd.Add("post[days]", "2");
            pd.Add("post[mycatid]", "0");
            pd.Add("post[elite]", "1");
            pd.Add("answer", GetVcode());
            pd.Add("submit", " 提 交 ");
            string html = source.Post(post_url, pd.Read(), post_url + "?mid=5&action=add");
            html = source.Get(post_url + "?mid=5&status=2");
            if (html.IndexOf(title) != -1)
            {
                result.Code = 1;
                result.Content = url + "/sell/show-" + Regex.Match(html, "itemid=([0-9]+)\" class=\"t\">" + title + "</a>").Groups[1].Value + ".html";
            }
            return result;
        }
        //获取验证码
        private string GetVcode()
        {
            string html = source.Get(url + "/api/captcha.png.php?action=question&refresh=0.6232850684158494.js&_utm_source=203efa", url + "/member/my.php?mid=5&action=add");
            int a = Convert.ToInt32(Regex.Match(html, "HTML = \"([0-9]+)").Groups[1].Value);
            int b = Convert.ToInt32(Regex.Match(html, "HTML = \"[0-9]+.([0-9]+)=").Groups[1].Value);
            string fuhao = Regex.Match(html, "HTML = \"[0-9]+(.)").Groups[1].Value;
            switch (fuhao)
            {
                case "+":
                    {
                        return (a + b).ToString();
                    }
                case "-":
                    {
                        return (a - b).ToString();
                    }
                case "*":
                    {
                        return (a * b).ToString();
                    }
            }
            return "";
        }
        public string UpImage()
        {
            string html = "";
            SourceFormData sfd = new SourceFormData("gb2312");
            //头部数据
            sfd.Write("fid", "");
            sfd.Write("moduleid", "5");
            sfd.Write("from", "album");
            sfd.Write("old", "");
            sfd.Write("isremote", "0");
            sfd.Write("remote", "http://");
            //发送文件
            sfd.WriteFile("upalbum", RandomPicture.Get());
            //尾部数据
            sfd.Write("width", "100");
            sfd.Write("height", "100");
            //结尾
            html = source.Post(url + "/upload.php", sfd.GetContentType(), sfd.Read(), url + "/member/my.php?mid=5&action=add");
            return Regex.Match(html, "getAlbum\\(\"([^\"]+)\"").Groups[1].Value;
        }
    }
}
